package leetcode;

/**
 * @author Liangyaoyun
 */
public class leetCode108 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public TreeNode sortedArrayToBST(int[] nums) {
        TreeNode root=new TreeNode();
        return BuildTree(nums,0, nums.length-1);
    }
    public  TreeNode BuildTree(int[] nums,int left,int right){
        if (left>right){
            return null;
        }
        int middle=(left+right)/2;
        TreeNode root=new TreeNode(nums[middle]);
        root.left=BuildTree(nums,0,middle-1);
        root.right=BuildTree(nums,middle+1,right);
        return root;
    }
}
